﻿<cfscript>
/** 
* @hint "在线投票相关监听器"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.vote.voteListener" 
	output=false 
	depends="sessionAdvice, datetimeAdvice, sequenceQuestionRespondent, sequenceQuestionnaire, sequenceQuestionQuota" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 

	
	/**
	* @hint "标注学生指定调查问卷完成"
	* 
	* @event "MachII Event Object"
	*/
	public void function lockStudentQuestionnaire( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var studentId = sessionAdvice.getAutherUserID();
		var timeStamp = getdatetimeAdvice().getCurrentDatetimeString();
		
		var sequenceQuestionRespondent = getSequenceQuestionRespondent();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			
			sql = "	SELECT
						stu.stu_id
					FROM
						t_student stu
					WHERE
						stu.stu_id = :studentId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			
			queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
			
			rs_student = queryObj.execute(sql=sql).getResult();
			
			sequenceQuestionRespondent.setQuestionID(questionId);
			sequenceQuestionRespondent.setStudentID(studentId);
			
			var responderId = sequenceQuestionRespondent.getID();
			
			sql = "	SELECT
						r.rsp_id
					FROM
						t_respondents r
					WHERE
						r.rsp_id = :responderId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name="responderId", value=responderId, cfsqltype="cf_sql_varchar");
			rs_resp = queryObj.execute(sql=sql).getResult();
			
			if(rs_resp.recordCount){
				
				sql = "	UPDATE t_respondents r
							 SET
								r.state = :state ,
								r.last_update = :lastUpdate 
							 WHERE
								r.rsp_id = :responderId ";
				
				updateObj = new Query(datasource = application.dnsMaster);
				
				updateObj.addParam(name="responderId", value=responderId, cfsqltype="cf_sql_varchar");
				updateObj.addParam(name="state", value="S", cfsqltype="cf_sql_char");
				updateObj.addParam(name="lastUpdate", value=timeStamp, cfsqltype="cf_sql_char");
				
				updateObj.execute(sql=sql);
				
			}else{
				sql ="INSERT INTO 
						t_respondents(
							rsp_id,
							qsn_id,
							stu_id,
							state,
							last_update
						)VALUES(
							:responderId ,
							:questionId ,
							:studentId ,
							:state ,
							:lastUpdate 	
						)";

				updateObj = new Query(datasource = application.dnsMaster);
				
				updateObj.addParam(name="responderId", value=responderId, cfsqltype="cf_sql_varchar");
				updateObj.addParam(name="questionId", value=questionId, cfsqltype="cf_sql_char");
				updateObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
				updateObj.addParam(name="state", value="S", cfsqltype="cf_sql_char");
				updateObj.addParam(name="lastUpdate", value=timeStamp, cfsqltype="cf_sql_char");
				
				updateObj.execute(sql=sql);
			}
		}
		
	}

	
	/**
	* @hint "交换问卷题目前后顺序"
	* 
	* @event "MachII Event Object"
	*/
	public void function swapQuota( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		
		var quotaId = event.getArg("QuotaID");
		var quotaIdNext = event.getArg("NextQuotaID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						q.quo_idx
					FROM
						t_quota q
					WHERE
						q.quo_id = :quotaId ";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaId, cfsqltype = "cf_sql_char");
			rs_quotaEnrity = queryObj.execute(sql = sql).getResult();

			sql = "	SELECT
						q.quo_idx
					FROM
						t_quota q
					WHERE
						q.quo_id = :quotaId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaIdNext, cfsqltype = "cf_sql_char");
			rs_quotaEnrityNext = queryObj.execute(sql = sql).getResult();
			
			var Idxs = [ rs_quotaEnrityNext.quo_idx, rs_quotaEnrity.quo_idx ];
			
			sql = "	UPDATE
						t_quota q
					SET
						q.quo_idx = :Idx 
					WHERE
						q.quo_id = :quotaId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaIdNext, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "Idx", value = Idxs[2], cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "Idx", value = Idxs[1], cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
			
		}
		
	}
	
	/**
	* @hint "删除问卷题目"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteQuota( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var quotaId = event.getArg("QuotaID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			sql = "	DELETE FROM 
						t_quota q
					WHERE
						q.quo_id = :quotaId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
				
		}
		
	}


	/**
	* @hint "更新问卷题目"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateQuota( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var quotaId = event.getArg("QuotaID");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	UPDATE
						t_quota q
					SET
						q.question = :question,
						q.measure = :measure 
					WHERE
						q.quo_id = :quotaId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "measure", value = event.getArg("Measure"), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "question", value = event.getArg("Question"), cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
			
		}
		
	}
	
	
	/**
	* @hint "添加问卷题目"
	* 
	* @event "MachII Event Object"
	*/
	public void function createQuota( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var sequenceQuestionQuota = getsequenceQuestionQuota();

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sequenceQuestionQuota.setQuestionID(questionId);
			
			quotaId = sequenceQuestionQuota.getID();
			
			sql = "	INSERT INTO t_quota(
						quo_id,
						qsn_id,
						quo_idx,
						question,
						measure
					)VALUES(
						:quotaId,
						:questionId,
						:idx,
						:question,
						:measure 
					)";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "quotaId", value = quotaId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "idx", value = right(quotaId, 3), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "question", value = event.getArg("Question"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "measure", value = event.getArg("Measure"), cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
			
		}
		
	}
	
	
	/**
	* @hint "发布在线投票问卷"
	* 
	* @event "MachII Event Object"
	*/
	public void function publishQuestionnaire( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var teacherId = getsessionAdvice().getAutherUserID();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	UPDATE
						t_questionnaire q
					SET
						q.tch_id = :teacherId ,
						q.state = '2'
					WHERE
						q.qsn_id = :questionId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "teacherId", value = teacherId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);

		}
		
	}
	
	
	/**
	* @hint "删除在线投票问卷"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteQuestionnaire( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	DELETE FROM
						t_questionnaire q
					WHERE
						q.qsn_id = :questionId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
		}
		
	}


	/**
	* @hint "更新在线投票问卷"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateQuestionnaire( required MachII.framework.Event event ) output=false {
		
		var questionId = event.getArg("QueID");
		var teacherId = getsessionAdvice().getAutherUserID();
		
		var timeStamp = getdatetimeAdvice().getCurrentDatetimeString();
		
		var sequenceQuestionRespondent = getsequenceQuestionRespondent();
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var dateStart = replace(event.getArg("DateStart"), "-", "", "ALL");
			var dateEnd = replace(event.getArg("DateEnd"), "-", "", "ALL");
			var title = event.getArg("Title");
			var describe = event.getArg("Describe");
			
			var state = "1";
			
			var gradesAllowed = instituteAllowed = genderAllowed = "";
			
			/* 允许投票年级 */
			if ( event.getArg("FullGrade") eq "1" ) {
				gradesAllowed = "ALL";
			} else {
				gradesAllowed = event.getArg('targetGrade');
			}
			
			/* 允许投票学院 */
			if ( event.getArg("FullInstitute") eq "1" ) {
				instituteAllowed = "ALL";
			} else {
				instituteAllowed = event.getArg('targetInstitute');
			}
			
			/* 允许投票性别 */
			if ( event.getArg("FullGender") eq "1" ) {
				genderAllowed = "ALL";
			} else if ( event.getArg('targetGender') eq "1,2" or event.getArg('targetGender') eq "2,1" ) {
				genderAllowed = "ALL";
			} else {
				genderAllowed = event.getArg('targetGender');
			}
			
			if ( event.getArg("SpecialStudent") eq "1" and event.getArg("StudentIDs") neq "NONE" ) {


				gradesAllowed = "NOP";

				instituteAllowed = "NOP";

				genderAllowed = "NOP";
				
				
				
			}
			
			/*此处原来EntitySave在括号内*/
			
			sql = "	UPDATE
						t_questionnaire q
					SET
						q.tch_id = :teacherId ,
						q.date_start = :dateStart,
					q.date_end = :dateEnd,
						q.title = :title,
						q.describe = :describe,
						q.grade_allowed = :gradesAllowed,
						q.institute_allowed = :instituteAllowed,
						q.gender_allowed = :genderAllowed,
						q.state = :state 
					WHERE
						q.qsn_id = :questionId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "teacherId", value = teacherId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "dateStart", value = dateStart, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateEnd", value = dateEnd, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "title", value = title, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "describe", value = describe, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "gradesAllowed", value = gradesAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "instituteAllowed", value = instituteAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "genderAllowed", value = genderAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "state", value = state, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);

			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
			
		}
		
		transaction {
			
			/* 清除未投票的预制名单 */
			sql = "	DELETE FROM
						t_respondents t
					WHERE
						t.qsn_id = :questionId 
						AND
						t.state = 'U'";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
		}
		
		transaction {
			
			/* 若设置为指定学生可投票 */
			if ( event.getArg("SpecialStudent") eq "1" and event.getArg("StudentIDs") neq "NONE" ) {

				var studentIds = listToArray(event.getArg("StudentIDs"), ", " & chr(10) & chr(13));
				
				/* 根据学号 批量设置可投票名单 */
				for ( var studentId in studentIds ) {
					
					sql = "	SELECT
								stu.stu_id
							FROM
								t_student stu
							WHERE
								stu.stu_id = :studentId ";
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
					rs_studentEntity = queryObj.execute(sql = sql).getResult();
					
					if ( rs_studentEntity.recordCount ) {
						
						sequenceQuestionRespondent.setStudentID(rs_studentEntity.stu_id);
						sequenceQuestionRespondent.setQuestionID(questionId);
					
						var responderId = sequenceQuestionRespondent.getID();
						
						sql = "	SELECT
									r.rsp_id
								FROM
									t_respondents r
								WHERE
									r.rsp_id = :responderId ";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "responderId", value = responderId, cfsqltype = "cf_sql_varchar");
						rs_respEntity = queryObj.execute(sql = sql).getResult();
						
						if ( rs_respEntity.recordCount eq 0 ) {

							sql = "	INSERT INTO t_respondents(
										rsp_id,
										qsn_id,
										stu_id,
										state,
										last_update
									)VALUES(
										:responderId,
										:questionId,
										:studentId,
										:state,
										:lastUpdate 
									)";
							
							queryObj = new Query(datasource = application.dnsMaster);
							queryObj.addParam(name = "responderId", value = responderId, cfsqltype = "cf_sql_varchar");
							queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
							queryObj.addParam(name = "studentId", value = rs_studentEntity.stu_id, cfsqltype = "cf_sql_varchar");
							queryObj.addParam(name = "state", value = "U", cfsqltype = "cf_sql_char");
							queryObj.addParam(name = "lastUpdate", value = timeStamp, cfsqltype = "cf_sql_char");
							queryObj.execute(sql = sql);
							
						}
						
						/*
						if ( isNull(respEntityCheck) ) {
							EntitySave( respEntity );
						}
						*/
						
					}
					
				}
			}
		}
		
	}
	
	
	/**
	* @hint "添加在线投票问卷"
	* 
	* @event "MachII Event Object"
	*/
	public void function createQuestionnaire( required MachII.framework.Event event ) output=false {
		
		var teacherId = getsessionAdvice().getAutherUserID();
		
		var timeStamp = getdatetimeAdvice().getCurrentDatetimeString();
		
		var sequenceQuestionnaire = getsequenceQuestionnaire();
		var sequenceQuestionRespondent = getsequenceQuestionRespondent();
		
		var sql = queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 生成主键 */
			var questionId = sequenceQuestionnaire.getID();
			
			/* 允许投票年级 */
			if ( event.getArg("FullGrade") eq "1" ) {
				gradesAllowed = "ALL";
			} else {
				gradesAllowed = event.getArg('targetGrade');
			}
			
			/* 允许投票学院 */
			if ( event.getArg("FullInstitute") eq "1" ) {
				instituteAllowed = "ALL";
			}
			else {
				instituteAllowed = event.getArg('targetInstitute');
			}
			
			/* 允许投票性别 */
			if ( event.getArg("FullGender") eq "1" ) {
				genderAllowed = "ALL";
			}
			else if ( event.getArg('targetGender') eq "1,2" or event.getArg('targetGender') eq "2,1" ) {
				genderAllowed = "ALL";
			}
			else {
				genderAllowed = event.getArg('targetGender');
			}
			
			/* 预保存投票 */

			sql = "	INSERT INTO t_questionnaire(
						qsn_id,
						tch_id,
						date_start,
						date_end,
						title,
						describe,
						grade_allowed,
						institute_allowed,
						gender_allowed,
						state
					)VALUES(
						:questionId,
						:createrTeacherId,
						:dateStart,
						:dateEnd,
						:title,
						:description,
						:gradeAllowed,
						:instituteAllowed,
						:genderAllowed,
						:state 
					)";
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "createrTeacherId", value = teacherId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "dateStart", value = replace(event.getArg("DateStart"), "-", "", "ALL"), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateEnd", value = replace(event.getArg("DateEnd"), "-", "", "ALL"), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "title", value = event.getArg("Title"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "description", value = event.getArg("Describe"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "gradeAllowed", value = gradesAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "instituteAllowed", value = instituteAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "genderAllowed", value = genderAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "state", value = '1', cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			/* 若设置为指定学生可投票 */
			if ( event.getArg("SpecialStudent") eq "1" and event.getArg("StudentIDs") neq "NONE" ) {
				
				sql = "	UPDATE
							t_questionnaire t
						SET
							t.grade_allowed = :gradeAllowed ,
							t.institute_allowed = :instituteAllowed ,
							t.gender_allowed = :genderAllowed 
						WHERE
							t.qsn_id = :questionId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "gradeAllowed", value = "NOP", cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "instituteAllowed", value = "NOP", cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "genderAllowed", value = "NOP", cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				var studentIds = listToArray(event.getArg("StudentIDs"), ", " & chr(10) & chr(13));
				
				/* 根据学号 批量设置可投票名单 */
				for ( var studentId in studentIds ) {
					
					sql = "	SELECT
								stu.stu_id
							FROM
								t_student stu
							WHERE
								stu.stu_id = :studentId ";
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
					rs_studentEntity = queryObj.execute(sql = sql).getResult();
					
					if ( rs_studentEntity.recordCount ) {
					
						sequenceQuestionRespondent.setStudentID(rs_studentEntity.stu_id);
						sequenceQuestionRespondent.setQuestionID(questionId);
					
						responderId = sequenceQuestionRespondent.getID();
						
						sql = "	SELECT
									r.rsp_id
								FROM
									t_respondents r
								WHERE
									r.rsp_id = :responderId ";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "responderId", value = responderId, cfsqltype = "cf_sql_varchar");
						rs_respEntity = queryObj.execute(sql = sql).getResult();
						
						if ( rs_respEntity.recordCount eq 0 ) {

							sql = "	INSERT INTO t_respondents(
										rsp_id,
										qsn_id,
										stu_id,
										state,
										last_update
									)VALUES(
										:responderId,
										:questionId,
										:studentId,
										:state,
										:lastUpdate 
									)";
							
							queryObj = new Query(datasource = application.dnsMaster);
							queryObj.addParam(name = "responderId", value = responderId, cfsqltype = "cf_sql_varchar");
							queryObj.addParam(name = "questionId", value = questionId, cfsqltype = "cf_sql_char");
							queryObj.addParam(name = "studentId", value = studentId, cfsqltype = "cf_sql_varchar");
							queryObj.addParam(name = "state", value = "U", cfsqltype = "cf_sql_char");
							queryObj.addParam(name = "lastUpdate", value = timeStamp, cfsqltype = "cf_sql_char");
							queryObj.execute(sql = sql);
							
						}
						
					}
					
				}
			}
			
			var passArgs = structNew();
			structInsert( passArgs, "QSN", questionId, true );

			event.setArgs( passArgs );
			
			
		}
		
	}
	
}

</cfscript>